#include "simplegraphicbutton.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QPalette>

SimpleGraphicButton::SimpleGraphicButton(QWidget *parent) :
    QPushButton(parent)
{
    setStyleSheet("SimpleGraphicButton {background-color:green}");
    this->setText(QString("未定义"));

    geo_descrption = new QLabel("", this);
    geo_descrption->setAlignment(Qt::AlignCenter);
    start_led = new QLabel(this);
    fedback_led = new QLabel(this);
    fault_led = new QLabel(this);

    QFont ft;
    ft.setPointSize(8);
    geo_descrption->setFont(ft);
    start_led->setFont(ft);
    fedback_led->setFont(ft);
    fault_led->setFont(ft);

    //设置layout策略
    QSizePolicy policy = sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    setSizePolicy(policy);

    policy = geo_descrption->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    geo_descrption->setSizePolicy(policy);

    policy = start_led->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    start_led->setSizePolicy(policy);

    policy = fedback_led->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    fedback_led->setSizePolicy(policy);

    policy = fault_led->sizePolicy();
    policy.setHorizontalPolicy(QSizePolicy::Expanding);
    policy.setVerticalPolicy(QSizePolicy::Expanding);
    fault_led->setSizePolicy(policy);

    //布局管理
    QVBoxLayout *toplayout = new QVBoxLayout;
    toplayout->addWidget(geo_descrption);
    QHBoxLayout *bottomlayout = new QHBoxLayout;
    bottomlayout->addWidget(start_led);
    bottomlayout->addWidget(fedback_led);
    bottomlayout->addWidget(fault_led);

    QVBoxLayout *mainlayout = new QVBoxLayout;
    mainlayout->addLayout(toplayout);
    mainlayout->addLayout(bottomlayout);
    setLayout(mainlayout);
}

void SimpleGraphicButton::init_const_member()
{
    this->setText("");
}

void SimpleGraphicButton::set_geo_description_and_num(const QString &geo_inf)
{
    geo_descrption->setText(geo_inf);
}

void SimpleGraphicButton::set_start_led(led_mode flag)
{
    start_led->setText(QString("启动"));
    if(flag == TURN_OFF)
    {
        start_led->setStyleSheet("QLabel{background-color:gray;}");
    }
    else if(flag == TURN_ON)
    {
        start_led->setStyleSheet("QLabel{background-color:red;}");
    }
    else if(flag == BLINK)
    {}
    else
    {
        start_led->setStyleSheet("QLabel{background-color:green;}");
    }
}


void SimpleGraphicButton::set_fedback_led(led_mode flag)
{
    fedback_led->setText(QString("反馈"));
    if(flag == TURN_OFF)
    {
        fedback_led->setStyleSheet("QLabel{background-color:gray;}");
    }
    else if(flag == TURN_ON)
    {
        fedback_led->setStyleSheet("QLabel{background-color:red;}");
    }
    else if(flag == BLINK)
    {}
    else
    {
        fedback_led->setStyleSheet("QLabel{background-color:green;}");
    }
}

void SimpleGraphicButton::set_fault_led(led_mode flag)
{
    fault_led->setText(QString("故障"));
    if(flag == TURN_OFF)
    {
        fault_led->setStyleSheet("QLabel{background-color:gray;}");
    }
    else if(flag == TURN_ON)
    {
        fault_led->setStyleSheet("QLabel{background-color:yellow;}");
    }
    else if(flag == BLINK)
    {}
    else
    {
        fault_led->setStyleSheet("QLabel{background-color:green;}");
    }
}

void SimpleGraphicButton::set_undefined_state()
{
//    this->setText("未定义");
    set_geo_description_and_num("未定义");
    set_start_led(UNDEFINED);
    set_fedback_led(UNDEFINED);
    set_fault_led(UNDEFINED);
    start_led->setText("");
    fedback_led->setText("");
    fault_led->setText("");
}

